package cn.swing.ui;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.common.CharacterSetECI;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import cn.configure.SettingConfigure;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
/**
 * @author xbsoft2
 */
public final class Setting extends JDialog {

  public Setting() {
    setContentPane(contentPane);
    setModal(true);
    setTitle("设置");
    setSize(550,400);
    setLocation(200,200);
    getRootPane().setDefaultButton(oK);
    setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    addWindowListener(new WindowAdapter() {
      @Override
      public void windowClosing(WindowEvent e) {
        dispose();
      }
    });
    contentPane.registerKeyboardAction(e -> dispose(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    initValue();
    initListener();
  }
  private JPanel contentPane;
  private JButton oK;
  private JButton cancel;
  /**
   *  配置参数
   */
  private SettingConfigure settingConfigure;
  /**
   *  二维码参数
   */
  private JTextField qrContents;
  private JSpinner qrWidth;
  private JSpinner qrHeight;
  private JButton qrOnColor;
  private JButton qrOffColor;
  private JTextField qrOnColorRGB;
  private JTextField qrOffColorHex;
  private JTextField qrOnColorHex;
  private JTextField qrOffColorRGB;
  private JSpinner qrX;
  private JSpinner qrY;
  private JComboBox<ErrorCorrectionLevel> qrErrorCorrectionLevel;
  private JComboBox<Integer> qrMargin;
  private JComboBox<BarcodeFormat> qrBarcodeFormat;
  private JComboBox<CharacterSetECI> qrCharacter;
  private JTextField logoPath;
  private JButton logoBut;
  private JCheckBox enableLogo;

  /**
   *  文字参数
   */
  private JCheckBox isShowText;
  private JTextField textContents;
  private JTextField textPrefix;
  private JTextField textSuffix;
  private JComboBox<String> textTypeName;
  private JComboBox<Integer> textStyle;
  private JComboBox<Integer> textSize;
  private JButton textColor;
  private JTextField textColorHex;
  private JTextField textColorRGB;
  private JSpinner textX;
  private JSpinner textY;
  private JComboBox textPosition;

  /**
   *  画布参数
   */
  private JSpinner canvasWidth;
  private JSpinner canvasHeight;
  private JButton canvasColor;
  private JTextField canvasColorHex;
  private JTextField canvasColorRGB;
  /**
   *  保存参数
   */
  private JTextField showDirectory;
  private JButton directory;
  private JTextField fileName;
  private JComboBox<String> fileFormat;
  /**
   *  其他参数
   */
  private JTextPane textInfo;


    /**
   * 1/4、初始化参数默认值
   */
  private void initValue() {
    try {
      // 初始化下拉框选项值
      initSelectList();
      // 获取配置参数
      settingConfigure = SettingConfigure.getSettingConfigure();
      // 初始化二维码参数
      qrContents.setText(settingConfigure.getQrCodeSetting().getContents());
      qrWidth.setValue(settingConfigure.getQrCodeSetting().getWidth());
      qrHeight.setValue(settingConfigure.getQrCodeSetting().getHeight());
      qrOnColor.setIcon(new Icon() {
        @Override
        public void paintIcon(Component c, Graphics g, int x, int y) {
          g.setColor(new Color(settingConfigure.getQrCodeSetting().getOnColor()));
          g.fillRect(x,y,qrOnColor.getWidth(),qrOnColor.getHeight());
        }
        @Override
        public int getIconWidth() {
          return qrOnColor.getWidth();
        }
        @Override
        public int getIconHeight() {
          return qrOnColor.getHeight();
        }
      });
      qrOnColorShow(new Color(settingConfigure.getQrCodeSetting().getOnColor()));
      qrOffColor.setIcon(new Icon() {
        @Override
        public void paintIcon(Component c, Graphics g, int x, int y) {
          g.setColor(new Color(settingConfigure.getQrCodeSetting().getOffColor()));
          g.fillRect(x,y,qrOffColor.getWidth(),qrOffColor.getHeight());
        }
        @Override
        public int getIconWidth() {
          return qrOffColor.getWidth();
        }
        @Override
        public int getIconHeight() {
          return qrOffColor.getHeight();
        }
      });
      qrOffColorShow(new Color(settingConfigure.getQrCodeSetting().getOffColor()));
      qrX.setValue(settingConfigure.getQrCodeSetting().getX());
      qrY.setValue(settingConfigure.getQrCodeSetting().getY());
      qrErrorCorrectionLevel.setSelectedItem(settingConfigure.getQrCodeSetting().getHints().get(EncodeHintType.PDF417_DIMENSIONS));
      qrCharacter.setSelectedItem(settingConfigure.getQrCodeSetting().getHints().get(EncodeHintType.CHARACTER_SET));
      qrMargin.setSelectedItem(settingConfigure.getQrCodeSetting().getHints().get(EncodeHintType.MARGIN));
      qrBarcodeFormat.setSelectedItem(settingConfigure.getQrCodeSetting().getBarcodeFormat());
      logoPath.setText(settingConfigure.getQrCodeSetting().getLogo());
      enableLogo.setSelected(settingConfigure.getQrCodeSetting().getEnableLogo());
      // 初始化字体参数
      isShowText.setSelected(settingConfigure.getTextSetting().getIsShow());
      textContents.setText(settingConfigure.getTextSetting().getContents());
      textPrefix.setText(settingConfigure.getTextSetting().getPrefix());
      textSuffix.setText(settingConfigure.getTextSetting().getSuffix());
      textTypeName.setSelectedItem(settingConfigure.getTextSetting().getFontName());
      textStyle.setSelectedItem(settingConfigure.getTextSetting().getFontStyle());
      textSize.setSelectedItem(settingConfigure.getTextSetting().getFontSize());
      textColor.setIcon(new Icon() {
        @Override
        public void paintIcon(Component c, Graphics g, int x, int y) {
          g.setColor(new Color(settingConfigure.getTextSetting().getColor()));
          g.fillRect(x,y,textColor.getWidth(),textColor.getHeight());
        }
        @Override
        public int getIconWidth() {
          return textColor.getWidth();
        }
        @Override
        public int getIconHeight() {
          return textColor.getHeight();
        }
      });
      textColorShow(new Color(settingConfigure.getTextSetting().getColor()));
      textX.setValue(settingConfigure.getTextSetting().getX());
      textY.setValue(settingConfigure.getTextSetting().getY());
      textPosition.setSelectedItem("其他");
      // 初始化画布参数
      canvasWidth.setValue(settingConfigure.getCanvasSetting().getWidth());
      canvasHeight.setValue(settingConfigure.getCanvasSetting().getHeight());
      canvasColor.setIcon(new Icon() {
        @Override
        public void paintIcon(Component c, Graphics g, int x, int y) {
          g.setColor(new Color(settingConfigure.getCanvasSetting().getColor()));
          g.fillRect(x,y,canvasColor.getWidth(), canvasHeight.getHeight());
        }
        @Override
        public int getIconWidth() {
          return canvasColor.getWidth();
        }
        @Override
        public int getIconHeight() {
          return canvasColor.getHeight();
        }
      });
      canvasColorShow(new Color(settingConfigure.getCanvasSetting().getColor()));
      // 初始化保存参数
      showDirectory.setText(settingConfigure.getSaveSetting().getDirectory());
      fileName.setText(settingConfigure.getSaveSetting().getFileName());
      fileFormat.setSelectedItem(settingConfigure.getSaveSetting().getFormatName());
      // 初始化其他参数
      textInfo.setText(settingConfigure.getOtherSetting().getInfo());
    }
    catch (IOException | ClassNotFoundException e) {
      e.printStackTrace();
      new Error("错误信息:\n"+e.getMessage());
    }
  }

  /**
   * 2/4、初始化下拉框选项值
   */
  private void initSelectList() {
    // 初始化二维码下拉框选项值
    qrErrorCorrectionLevel.addItem(ErrorCorrectionLevel.L);
    qrErrorCorrectionLevel.addItem(ErrorCorrectionLevel.M);
    qrErrorCorrectionLevel.addItem(ErrorCorrectionLevel.Q);
    qrErrorCorrectionLevel.addItem(ErrorCorrectionLevel.H);
    qrCharacter.addItem(CharacterSetECI.UTF8);
    qrCharacter.addItem(CharacterSetECI.ASCII);
    qrCharacter.addItem(CharacterSetECI.ISO8859_16);
    qrCharacter.addItem(CharacterSetECI.GB18030);
    qrMargin.addItem(-1);
    qrMargin.addItem(0);
    qrMargin.addItem(1);
    qrMargin.addItem(2);
    qrMargin.addItem(3);
    qrBarcodeFormat.addItem(BarcodeFormat.QR_CODE);
    qrBarcodeFormat.addItem(BarcodeFormat.ITF);
    qrBarcodeFormat.addItem(BarcodeFormat.AZTEC);
    qrBarcodeFormat.addItem(BarcodeFormat.CODABAR);
    qrBarcodeFormat.addItem(BarcodeFormat.CODE_39);
    qrBarcodeFormat.addItem(BarcodeFormat.DATA_MATRIX);
    qrBarcodeFormat.addItem(BarcodeFormat.EAN_8);
    qrBarcodeFormat.addItem(BarcodeFormat.RSS_EXPANDED);
    qrBarcodeFormat.addItem(BarcodeFormat.UPC_A);
    // 初始化字体下拉框选项值
    textTypeName.addItem(Font.DIALOG);
    textTypeName.addItem(Font.DIALOG_INPUT);
    textTypeName.addItem(Font.MONOSPACED);
    textTypeName.addItem(Font.SANS_SERIF);
    textTypeName.addItem(Font.SERIF);
    textTypeName.addItem("微软雅黑");
    textStyle.addItem(Font.PLAIN);
    textStyle.addItem(Font.BOLD);
    textStyle.addItem(Font.ITALIC);
    textPosition.addItem("其他");
    textPosition.addItem("上");
    textPosition.addItem("下");
    for (int i = 10; i < 50; i++) {textSize.addItem(i);}
    // 初始化保存下拉框选项值 webp，bmp，jpg，png，tif，gif，apng等
    fileFormat.addItem("png");
    fileFormat.addItem("icon");
    fileFormat.addItem("jpg");
    fileFormat.addItem("jpeg");
    fileFormat.addItem("webp");
    fileFormat.addItem("bmp");
    fileFormat.addItem("gif");
    fileFormat.addItem("apng");
    fileFormat.addItem("dds");
    fileFormat.addItem("psd");
    fileFormat.addItem("pdt");
    fileFormat.addItem("xmp");
    fileFormat.addItem("svg");
    fileFormat.addItem("tif");
  }
  /**
   * 3/4、初始化监听器
   */
  private void initListener() {
    oK.addActionListener(e -> onOK());
    cancel.addActionListener(e -> dispose());
    qrOnColor.addActionListener(e -> selectQrOnColor());
    qrOffColor.addActionListener(e -> selectQrOffColor());
    textColor.addActionListener(e -> selectTextColor());
    canvasColor.addActionListener(e -> selectCanvasColor());
    directory.addActionListener(e -> selectDirectory());
    logoBut.addActionListener(e -> selectLogo());
    textPosition.addActionListener(e -> selectTextPosition(e));
  }

  /**
   * 4/4、保存设置参数
   */
  private void onOK() {

    // 设置二维码参数
    settingConfigure.getQrCodeSetting().setContents(qrContents.getText());
    settingConfigure.getQrCodeSetting().setWidth(Integer.valueOf(qrWidth.getValue().toString()));
    settingConfigure.getQrCodeSetting().setHeight(Integer.valueOf(qrHeight.getValue().toString()));
    settingConfigure.getQrCodeSetting().setX(Integer.valueOf(qrX.getValue().toString()));
    settingConfigure.getQrCodeSetting().setY(Integer.valueOf(qrY.getValue().toString()));
    settingConfigure.getQrCodeSetting().getHints().put(EncodeHintType.PDF417_DIMENSIONS, qrErrorCorrectionLevel.getSelectedItem());
    settingConfigure.getQrCodeSetting().getHints().put(EncodeHintType.CHARACTER_SET, qrCharacter.getSelectedItem());
    settingConfigure.getQrCodeSetting().getHints().put(EncodeHintType.MARGIN, qrMargin.getSelectedItem());
    settingConfigure.getQrCodeSetting().setBarcodeFormat((BarcodeFormat) qrBarcodeFormat.getSelectedItem());
    settingConfigure.getQrCodeSetting().setEnableLogo(enableLogo.isSelected());
    // 设置文字参数
    settingConfigure.getTextSetting().setIsShow(isShowText.isSelected());
    settingConfigure.getTextSetting().setContents(textContents.getText());
    settingConfigure.getTextSetting().setPrefix(textPrefix.getText());
    settingConfigure.getTextSetting().setSuffix(textSuffix.getText());
    settingConfigure.getTextSetting().setFontName(textTypeName.getSelectedItem().toString());
    settingConfigure.getTextSetting().setFontStyle(Integer.valueOf(textStyle.getSelectedItem().toString()));
    settingConfigure.getTextSetting().setFontSize(Integer.valueOf(textSize.getSelectedItem().toString()));
    settingConfigure.getTextSetting().setX(Integer.valueOf(textX.getValue().toString()));
    settingConfigure.getTextSetting().setY(Integer.valueOf(textY.getValue().toString()));
    // 设置画布参数
    settingConfigure.getCanvasSetting().setWidth(Integer.valueOf(canvasWidth.getValue().toString()));
    settingConfigure.getCanvasSetting().setHeight(Integer.valueOf(canvasHeight.getValue().toString()));
    // 设置保存参数
    settingConfigure.getSaveSetting().setFileName(fileName.getText());
    settingConfigure.getSaveSetting().setFormatName(fileFormat.getSelectedItem().toString());
    // 设置其他参数
    settingConfigure.getOtherSetting().setInfo(textInfo.getText());
    // 保存设置
    try {
      SettingConfigure.saveSettingConfigure();
      dispose();
    }
    catch (IOException e) {
      e.printStackTrace();
      new Error("保存设置失败:\n"+e.getMessage());
    }
  }
  /**
   * 二维码前景色选择
   */
  private void selectQrOnColor() {
    Color color = new Color(settingConfigure.getQrCodeSetting().getOnColor());
    color = JColorChooser.showDialog(null, "选取颜色", color);
    if (null != color){
      settingConfigure.getQrCodeSetting().setOnColor(color.getRGB());
      qrOnColor.setIcon(new Icon() {
        @Override
        public void paintIcon(Component c, Graphics g, int x, int y) {
          g.setColor(new Color(settingConfigure.getQrCodeSetting().getOnColor()));
          g.fillRect(x,y,qrOnColor.getWidth(),qrOnColor.getHeight());
        }
        @Override
        public int getIconWidth() {
          return qrOnColor.getWidth();
        }
        @Override
        public int getIconHeight() {
          return qrOnColor.getHeight();
        }
      });
      qrOnColorShow(color);
    }
  }
  /**
   * 二维码背景色选择
   */
  private void selectQrOffColor() {
    Color color = new Color(settingConfigure.getQrCodeSetting().getOffColor());
    color = JColorChooser.showDialog(null, "选取颜色", color);
    if (null != color){
      settingConfigure.getQrCodeSetting().setOffColor(color.getRGB());
      qrOffColor.setIcon(new Icon() {
        @Override
        public void paintIcon(Component c, Graphics g, int x, int y) {
          g.setColor(new Color(settingConfigure.getQrCodeSetting().getOffColor()));
          g.fillRect(x,y,qrOffColor.getWidth(),qrOffColor.getHeight());
        }
        @Override
        public int getIconWidth() {
          return qrOffColor.getWidth();
        }
        @Override
        public int getIconHeight() {
          return qrOffColor.getHeight();
        }
      });
      qrOffColorShow(color);
    }
  }

  /**
   * 字体颜色选择
   */
  private void selectTextColor() {
    Color color = new Color(settingConfigure.getTextSetting().getColor());
    color = JColorChooser.showDialog(null, "选取颜色", color);
    if (null != color){
      settingConfigure.getTextSetting().setColor(color.getRGB());
      textColor.setIcon(new Icon() {
        @Override
        public void paintIcon(Component c, Graphics g, int x, int y) {
          g.setColor(new Color(settingConfigure.getTextSetting().getColor()));
          g.fillRect(x,y,textColor.getWidth(),textColor.getHeight());
        }
        @Override
        public int getIconWidth() {
          return textColor.getWidth();
        }
        @Override
        public int getIconHeight() {
          return textColor.getHeight();
        }
      });
      textColorShow(color);
    }
  }
  /**
   * 被选择的文字显示位置，之后调整文字位置和二维码位置
   * @param e e
   */
  private void selectTextPosition(ActionEvent e) {
    String position = textPosition.getSelectedItem().toString();
    if ("上".equals(position)){
      textX.setValue(80);textY.setValue(30);
      qrX.setValue(0);qrY.setValue(25);
    }
    if ("下".equals(position)){
      qrX.setValue(0);qrY.setValue(0);
      textX.setValue(80);textY.setValue(310);
    }
  }

  /**
   * 画布颜色选择
   */
  private void selectCanvasColor() {
    Color color = new Color(settingConfigure.getCanvasSetting().getColor());
    color = JColorChooser.showDialog(null, "选取颜色", color);
    if (null != color){
      settingConfigure.getCanvasSetting().setColor(color.getRGB());
      canvasColor.setIcon(new Icon() {
        @Override
        public void paintIcon(Component c, Graphics g, int x, int y) {
          g.setColor(new Color(settingConfigure.getCanvasSetting().getColor()));
          g.fillRect(x,y,canvasColor.getWidth(), canvasHeight.getHeight());
        }
        @Override
        public int getIconWidth() {
          return canvasColor.getWidth();
        }
        @Override
        public int getIconHeight() {
          return canvasColor.getHeight();
        }
      });
      canvasColorShow(color);
    }
  }

  /**
   * 选择保存目录
   */
  private void selectDirectory(){
    JFileChooser fileChooser = new JFileChooser(settingConfigure.getSaveSetting().getDirectory());
    fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    fileChooser.showOpenDialog(null);
    File selectedFile = fileChooser.getSelectedFile();
    if (selectedFile != null){
      String absolutePath = selectedFile.getAbsolutePath();
      showDirectory.setText(absolutePath);
      settingConfigure.getSaveSetting().setDirectory(absolutePath);
    }
  }
  /**
   * 选择logo
   */
  private void selectLogo(){
    JFileChooser fileChooser = new JFileChooser(settingConfigure.getSaveSetting().getDirectory());
    fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    fileChooser.showOpenDialog(null);
    File selectedFile = fileChooser.getSelectedFile();
    if (selectedFile != null){
      String absolutePath = selectedFile.getAbsolutePath();
      logoPath.setText(absolutePath);
      settingConfigure.getQrCodeSetting().setLogo(absolutePath);
    }
  }

  /**
   * 显示前景色数据
   * @param color color
   */
  private void qrOnColorShow(Color color) {
    qrOnColorHex.setText(Integer.toHexString(color.getRGB()));
    qrOnColorRGB.setText(color.getRed()+","+color.getGreen()+","+color.getBlue());
  }
  /**
   * 显示二维码背景色数据
   * @param color color
   */
  private void qrOffColorShow(Color color) {
    qrOffColorHex.setText(Integer.toHexString(color.getRGB()));
    qrOffColorRGB.setText(color.getRed()+","+color.getGreen()+","+color.getBlue());
  }
  /**
   * 显示文字颜色
   * @param color color
   */
  private void textColorShow(Color color) {
    textColorHex.setText(Integer.toHexString(color.getRGB()));
    textColorRGB.setText(color.getRed()+","+color.getGreen()+","+color.getBlue());
  }
  /**
   * 画布颜色
   * @param color color
   */
  private void canvasColorShow(Color color) {
    canvasColorHex.setText(Integer.toHexString(color.getRGB()));
    canvasColorRGB.setText(color.getRed()+","+color.getGreen()+","+color.getBlue());
  }
}
